/**
 * 104. 二叉树的最大深度
 * https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
 */

const { createTreeNode } = require('../../libs/public');

/**
 * @param {TreeNode} root
 * @return {number}
 */
function maxDepth(root) {
  if (!root) return 0;
  const stack = [root];
  let depth = 0;
  while (stack.length) {
    depth += 1;
    const { length } = stack;
    for (let i = 0; i < length; i += 1) {
      const node = stack.shift();
      if (node.left) stack.push(node.left);
      if (node.right) stack.push(node.right);
    }
  }
  return depth;
}

console.log(maxDepth(createTreeNode([3, 9, 20, null, null, 15, 7])) === 3);
console.log(maxDepth(createTreeNode([1])) === 1);
console.log(maxDepth(createTreeNode([])) === 0);
